package com.hsgene.logistics.redis.impl;

import com.hsgene.logistics.redis.LogisticsRedisPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * redis操作
 *
 * @Author: wangxuefeng@hsgene.com
 * @Description:
 * @Date: Created in 10:43 2018/7/26
 * @Modified By:
 */
@Repository
public class LogisticsRedisPersistenceImpl implements LogisticsRedisPersistence {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 保存es
     *
     * @param key             key
     * @param logisticsNumber 物流单号
     */
    @Override
    public void saveLogisticsNumber(String key, String logisticsNumber) {
        redisTemplate.opsForList().leftPush(key, logisticsNumber);
    }

    /**
     * 查询物流单号List
     *
     * @param logisticsKey redis key
     * @param start        开始位置
     * @param end          结束位置
     * @return
     */
    @Override
    public List<String> getLogisticsFromRedis(String logisticsKey, int start, int end) {
        return redisTemplate.opsForList().range(logisticsKey, start, end);
    }

    /**
     * 查询List
     *
     * @param logisticsKey redis key
     * @return
     */
    @Override
    public long getLogisticsSize(String logisticsKey) {
        return redisTemplate.opsForList().size(logisticsKey);
    }

    /**
     * 删除
     *
     * @param logisticsKey    key
     * @param logisticsNumber 物流单号
     */
    @Override
    public void remove(String logisticsKey, String logisticsNumber) {
        redisTemplate.opsForList().remove(logisticsKey, 0, logisticsNumber);
    }
}
